package controller;



import java.util.Date;
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.User;
import service.UserService;
import service.UserServiceImpl;
import service.UserServiceManager;



@Controller
//@RequestMapping("/test1")
public class TestController  {
	private UserService us;
	public TestController(){
		us=UserServiceManager.getServiceImpl();
	}
  
@RequestMapping(value={"/test1"})
//	@GetMapping
	public String test1(Model model){
		model.addAttribute("uname", "lionel");
		return "lionel";
	}

@RequestMapping(value="/test1",method=RequestMethod.GET)	
//	@PostMapping
	public String test2(Model model){
		model.addAttribute("uname", "weir  傻逼");
		return "lionel";
	}
	
	@RequestMapping(value="/test11",headers={"action=load","Content-Type=text/html"})
//	@RequestMapping(value="/test1",consumes={"application/json"},produces="application/json")
	public String test3(Model model){
		model.addAttribute("uname", "test3");
		return "lionel";
	}
	
	@RequestMapping(value="/test4")
	
	public String test4(@DateTimeFormat(pattern="yyyy-MM-dd")Date birth,Model model){
		
		System.out.println(birth);
		return "lionel";
	}
	
	@RequestMapping(value="/test4/")
	
	public String test6(@CookieValue(value="JSESSIONID") String key){
		
		System.out.println("key:"+key);
		return "lionel";
	}
	
	@RequestMapping(value="/test5",consumes="application/json")
	public String test8(@RequestBody User u,Model model){
		System.out.println(u.getName()+"      "+u.getAge()+"    "+u.getBirth());
		return "lionel";
	}
//	@ResponseBody
//	@RequestMapping(value="/test5",consumes="application/json")
//	public User test8(@RequestBody User u,Model model){
//		System.out.println(u.getName()+"      "+u.getAge()+"    "+u.getBirth());
//		return u;
//	}
	
	@RequestMapping(value="/test6",method=RequestMethod.POST)
	
	public String test9(User u){
		
		us.addUser(u);		
		System.out.println(u.getName()+"      "+u.getAge()+"    "+u.getSal()+"   "+u.getBirth());
		
		
		
//		return "forward:/listAll";  转发
		return "redirect:/listAll";  //重定向
	}
	
	@RequestMapping(value="/listAll")
	public String getAll(Model model){
		List<User> list = us.getUsers();
		model.addAttribute("users", list);
		return "show";
	}
	
	@ResponseBody    //通过response直接打印回去
	@RequestMapping(value="/test7")	
	public User test10(){
		User u=new User();
		u.setName("weir");
		u.setAge(123);
		u.setSal(12.23);
		u.setSex(true);
		u.setBirth(new Date());
		return u;  
	}

}
